﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using carShop.Models;
using carShop.Models.Enums;

namespace carShop.Data.Helpers
{
    public static class ContextHelpers
    {
        public static void ApplyStateChanges(this DbContext context)
        {
            foreach (var dbEntityEntry in context.ChangeTracker.Entries<IObjectState>())
            {
                //IObjectState stateInfo = dbEntityEntry.Entity.State;

                switch (dbEntityEntry.Entity.State)
                {
                    case State.Added:
                        dbEntityEntry.State = EntityState.Added;
                        break;
                    case State.Modified:
                        dbEntityEntry.State = EntityState.Modified;
                        break;
                    case State.Deleted:
                        dbEntityEntry.State = EntityState.Deleted;
                        break;
                    default:
                        dbEntityEntry.State = EntityState.Unchanged;
                        break;
                }
                
            }
        }
    }
}